import { Body, Controller, Delete, Get, Param, Post, Put, Query, UseGuards, UsePipes } from '@nestjs/common';
import { MenuService } from './menu.service';
import { ZodValidationPipe } from '../../common/pipes/zod-validation.pipe';
import {
  CreateMenuDto,
  UpdateMenuDto,
  createMenuSchema,
  updateMenuSchema,
  paramSchema,
} from './dto/menu.schema';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

@ApiTags('menu-菜单模块')
@Controller('menus')
@UseGuards(JwtAuthGuard)
export class MenuController {
  constructor(private readonly menuService: MenuService) { }

  @Post()
  @ApiOperation({ summary: '创建菜单' })
  @UsePipes(new ZodValidationPipe(createMenuSchema))
  create(@Body() createMenuDto: CreateMenuDto) {
    return this.menuService.create(createMenuDto);
  }

  @Get()
  @ApiOperation({ summary: '查询菜单列表' })
  findAll() {
    return this.menuService.findAll();
  }

  @Get('tree')
  @ApiOperation({ summary: '查询菜单树' })
  getMenuTree() {
    return this.menuService.getMenuTree();
  }

  @Get('role/:roleId')
  @ApiOperation({ summary: '根据角色id查询菜单列表' })
  getMenuListByRoleId(@Param('roleId') roleId: string) {
    return this.menuService.getMenuListByRoleId(+roleId);
  }

  @Get(':id')
  @ApiOperation({ summary: '根据id查询菜单' })
  @UsePipes(new ZodValidationPipe(paramSchema))
  findOne(@Param() params: { id: string }) {
    return this.menuService.findOne(+params.id);
  }

  @Put(':id')
  @ApiOperation({ summary: '根据id更新菜单' })
  @UsePipes(new ZodValidationPipe(paramSchema))
  update(
    @Param() params: { id: string },
    @Body(new ZodValidationPipe(updateMenuSchema)) updateMenuDto: UpdateMenuDto,
  ) {
    return this.menuService.update(+params.id, updateMenuDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '根据id删除菜单' })
  @UsePipes(new ZodValidationPipe(paramSchema))
  remove(@Param() params: { id: string }) {
    return this.menuService.remove(+params.id);
  }
} 